<?php

namespace app\api\controller;

use app\common\controller\Api;
use app\api\model\UserAddress as UserAddressModel;
use think\Db;
use think\Validate;

/**
 * 收货地址接口
 */
class Useraddress extends Api
{
    //protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];


    /**
     * 列表
     *
     */
    public function lists()
    {
        $list = UserAddressModel::where(['user_id'=>$this->auth->id])->order('is_default desc')->select();

        $this->success('请求成功',$list);
    }

    //添加
    public function add()
    {
        $account_model = new UserAddressModel();

        $data = [
            'user_id'=>$this->auth->id,
            'name'=>input('name',''),
            'mobile'=>input('mobile',''),
            'is_default'=>input('is_default',0),
            'address'=>input('address',''),
            'province_id'=>input('province_id',0),
            'city_id'=>input('city_id',0),
            'district_id'=>input('district_id',0),
            'full_address'=>input('full_address',''),
        ];
        if (empty($data['name']))$this->error('请输入姓名');
        if (empty($data['mobile']))$this->error('请输入手机号');
        if (empty($data['address']))$this->error('请输入详细地址');
        if (!Validate::regex($data['mobile'], "^1\d{10}$")) {
            $this->error('手机号格式错误');
        }
        if ($data['is_default']==1){
            UserAddressModel::update(['is_default'=>0],['user_id'=>$this->auth->id]);
        }
        $res = $account_model->save($data);

        $this->success('请求成功',$res);
    }

    //银行地址信息
    public function info()
    {
        $id = input('id',0);

        $info = UserAddressModel::get($id);

        if (!$info)$this->error('地址不存在');

        $this->success('请求成功',$info);
    }

    //删除
    public function del()
    {
        $id = input('id',0);

        $info = UserAddressModel::get($id);

        if (!$info)$this->error('地址不存在');

        $res = $info->delete();

        if ($res){
            $this->success('删除成功');
        } else {
            $this->error('删除失败');
        }
    }

    //编辑
    public function edit()
    {
        $id = input('id',0);

        $info = UserAddressModel::get($id);

        if (!$info)$this->error('地址不存在');
        $account_model = new UserAddressModel();
        $data = [
            'user_id'=>$this->auth->id,
            'name'=>input('name',''),
            'mobile'=>input('mobile',''),
            'is_default'=>input('is_default',0),
            'address'=>input('address',''),
            'province_id'=>input('province_id',0),
            'city_id'=>input('city_id',0),
            'district_id'=>input('district_id',0),
            'full_address'=>input('full_address',''),
        ];
        if (empty($data['name']))$this->error('请输入姓名');
        if (empty($data['mobile']))$this->error('请输入手机号');
        if (empty($data['address']))$this->error('请输入详细地址');
        if (!Validate::regex($data['mobile'], "^1\d{10}$")) {
            $this->error('手机号格式错误');
        }
        if ($data['is_default']==1){
            UserAddressModel::update(['is_default'=>0],['user_id'=>$this->auth->id]);
        }
        $res = $account_model->save($data,['id'=>$id]);

        $this->success('编辑成功');
    }

    //设置默认
    public function setDefault()
    {
        $id = input('id',0);
        $info = UserAddressModel::get($id);
        if (!$info)$this->error('地址不存在');
        UserAddressModel::update(['is_default'=>0],['user_id'=>$this->auth->id]);

        $info->is_default = 1;

        $info->save();

        $this->success('设置成功');
    }
}
